function getPrintOrders(){
    var api = "/backstage/print/orders";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200) {
                var printOrders = data.data;
                $("#printOrderDataTable").DataTable({
                    data: printOrders,
                    columnDefs: [
                        {
                            "targets": "printOrderSerialNum",
                            "data": "serial_num",
                        },
                        {
                            "targets": "printOrderContact",
                            "data": "contact",
                        },
                        {
                            "targets": "printOrderTotalPrice",
                            "data": "total_price",
                        },
                        {
                            "targets": "printOrderTotalShouldPay",
                            "data": "total_should_pay",
                        },
                        {
                            "targets": "printOrderStatus",
                            "data": "status",
                        },
                        {
                            "targets": "printOrderPayStatus",
                            "data": "pay_status",
                        },
                        {
                            "targets": "printOrderCreateTime",
                            "data": "create_time",
                        },
                        {
                            "targets": "printOrderOperation",
                            "defaultContent": 
                            '<button class="btn btn-primary btn-xs printOrderDetail">查看</button>' +
                            '<button class="btn btn-primary btn-xs printOrderDelete">删除</button>',
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    })
}



function initPrintOrderList(){
    getPrintOrders();
    $('#printOrderDataTable').on('click', 'button', function () {
        var table = $("#printOrderDataTable").DataTable();
        var printOrderID = table.row($(this).parents('tr')).data().id;
        var actionName = $(this)[0].classList[3];

        // var actionMap = {
        //     "printOrderDetail": "printOrder_detail.html",
        // }

        // var href = actionMap[actionName] + "?userID=" + userID;

        // window.location.href = href;
    } );
}

function init() {
    var parts = window.location.pathname.split("/");
    var lastPart = parts[parts.length - 1];

    var initFuncMap = {
        "print_order_list.html": initPrintOrderList,
    };

    var initFunc = initFuncMap[lastPart];
    initFunc();
}

$(function(){
    init();
})